import 'package:color_dart/color_dart.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertz/components/a_button/index.dart';
import 'package:fluttertz/components/a_dialog/a_dialog.dart';
import 'package:fluttertz/components/a_row/a_row.dart';
import 'package:fluttertz/jsonserialize/jtdetailinfo/data.dart';
import 'package:fluttertz/utils/global.dart';
import 'package:intl/intl.dart';

class Jtinfo extends StatefulWidget {
  Jtinfo({Key key}) : super(key: key);

  _JtinfoState createState() => _JtinfoState();
}

class _JtinfoState extends State<Jtinfo> with TickerProviderStateMixin {
  static TabController _tabController;

  static List<Tab> tabs = [
    Tab(
      text: '信息',
    ),
    Tab(
      text: "控制",
    ),
    Tab(
      text: "查帳",
    ),
  ];

  Jtdetailinfo _jtinfo = new Jtdetailinfo();
  bool _istop = false; //当前是否已置顶

  String _name = "";
  int _bjje = 0;
  int _zdxb = 0;

  int _hour = 0;
  int _miniute = 0;
  int _cost = 0;

  int _ztbs = 0; //总投币数
  int _zchs = 0; //总出货数
  String _xzrq0 = ""; //开始日期
  String _xzrq1 = ""; //结束日期

  int _tblj = 0;
  int _chlj = 0;

  @override
  void initState() {
    super.initState();

    _tabController = TabController(vsync: this, length: tabs.length);

    Future.delayed(Duration.zero, () {
      _init(context);
    });
  }

  _init(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/jtdetailinfo",
          queryParameters: {"id": G.user.jtinfo.id});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('獲取機台详情失敗！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('獲取機台详情失敗');
        G.loading.hide(context);
        return null; //返回值不对
      }

      _jtinfo = Jtdetailinfo.fromJson(resCallbackMap['data']);
      _name = _jtinfo.name;
      _bjje = _jtinfo.bjje;
      _zdxb = _jtinfo.zdxb;
      _hour = (_jtinfo.time / 60).toInt();
      _miniute = _jtinfo.time % 60;
      _cost = _jtinfo.cost;

      _controller_jtname = new TextEditingController(text: _name);
      _controller_bjje = new TextEditingController(text: "$_bjje");
      _controller_zdxb = new TextEditingController(text: "$_zdxb");
      _controller_hour = new TextEditingController(text: "$_hour");
      _controller_miniute = new TextEditingController(text: "$_miniute");
      _controller_cost = new TextEditingController(text: "$_cost");

      DateTime now = DateTime.now();
      String formattedDate = DateFormat('yyyy-MM-dd').format(now);
      _xzrq0 = formattedDate; //开始日期
      _xzrq1 = formattedDate; //结束日期

      int y = now.year;
      int m = now.month + 1;
      if (m > 12) {
         y = y + 1;
         m = 1;
      }
      var dateNextMonthDate = new DateTime(y, m, now.day);
      String nextMonthDate = DateFormat('yyyy-MM-dd').format(dateNextMonthDate);
      G.loading.hide(context);
      setState(() {});

      if (_jtinfo.start_date.isEmpty) {
        _jtinfo.expiration_date = "未正式上線";
      } else if (_jtinfo.expiration_date == "2099-12-31") {
        _jtinfo.expiration_date = "未限制";
      } else {
        if (formattedDate.compareTo(_jtinfo.expiration_date) > 0) {
          G.toastlong('此機台已到期，無法進行操作!');
          Navigator.of(context).pop();
          return null;
        } else if (nextMonthDate.compareTo(_jtinfo.expiration_date) > 0) {
          G.toastlong('此機台 ' + _jtinfo.expiration_date + ' 到期！');
        }
      }

      //先查详情后，再查账
      qingchu(context);
      doLjcx(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  TextEditingController _controller_jtname;
  TextEditingController _controller_bjje;
  TextEditingController _controller_zdxb;
  TextEditingController _controller_hour;
  TextEditingController _controller_miniute;

  TextEditingController _controller_ztbs;
  TextEditingController _controller_zchs;
  TextEditingController _controller_tblj;
  TextEditingController _controller_chlj;
  TextEditingController _controller_cost;

  //tab2 查询 总投币数 + 总出货数
  qingchu(BuildContext context) async {
    if (_controller_ztbs == null) {
      _controller_ztbs = new TextEditingController(text: "$_ztbs");
      _controller_zchs = new TextEditingController(text: "$_zchs");
    }
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post("https://aps.acciot.com.tw/api/taizhu/qingchu",
          queryParameters: {
            "id": G.user.jtinfo.id,
            "xzrq0": _xzrq0,
            "xzrq1": _xzrq1
          });

      if (response == null) {
        G.loading.hide(context);
        return G.toast('獲取機台详情失敗！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('獲取機台详情失敗');
        G.loading.hide(context);
        return null; //返回值不对
      }

      _ztbs = resCallbackMap['data']['ztbs'];
      _zchs = resCallbackMap['data']['zchs'];

      _controller_ztbs.text = "$_ztbs";
      _controller_zchs.text = "$_zchs";

      setState(() {});
      G.loading.hide(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  //tab2 累進獲取
  doLjcx(BuildContext context) async {
    if (_controller_tblj == null) {
      _controller_tblj = new TextEditingController(text: "$_tblj");
      _controller_chlj = new TextEditingController(text: "$_chlj");
    }

    try {
      Response response;
      Dio dio = Dio();
      response = await dio
          .post("https://aps.acciot.com.tw/api/taizhu/ljcx", queryParameters: {
        "id": G.user.jtinfo.id,
      });

      if (response == null) {
        return;
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        return;
      }

      _tblj = resCallbackMap['data']['ztbs'];
      _chlj = resCallbackMap['data']['zchs'];

      _controller_tblj.text = "$_tblj";
      _controller_chlj.text = "$_chlj";

      setState(() {});
    } catch (e) {}
  }

  //tab2 累進歸零
  doResettblj(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/resettblj",
          queryParameters: {"id": G.user.jtinfo.id});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('歸零失敗');
        G.loading.hide(context);
        return null; //返回值不对
      }
      _tblj = 0;
      _controller_tblj.text = "$_tblj";
      G.toast('歸零成功');
      setState(() {});
      G.loading.hide(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  doResetchlj(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/resetchljt",
          queryParameters: {"id": G.user.jtinfo.id});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('歸零失敗');
        G.loading.hide(context);
        return null; //返回值不对
      }
      _chlj = 0;
      _controller_chlj.text = "$_chlj";
      G.toast('歸零成功');
      setState(() {});
      G.loading.hide(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  doSetCost(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/jtcost",
          queryParameters: {"id": G.user.jtinfo.id, "cost": _cost});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('設置商品成本失敗');
        G.loading.hide(context);
        return null; //返回值不对
      }
      _controller_cost.text = "$_cost";
      G.toast('設置商品成本成功');
      setState(() {});
      G.loading.hide(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  /// 创建appbar
  AppBar createAppBar() {
    /// 配置appbar
    return AppBar(
      centerTitle: true,
      leading: InkWell(
        child: icontupian8(color: rgba(44, 44, 44, 1), size: 16),
        onTap: () => Navigator.pop(context),
      ),
      title: Text(
        "$_name",
        style: TextStyle(
            color: rgba(56, 56, 56, 1),
            fontSize: 18,
            fontWeight: FontWeight.bold),
      ),
      bottom: PreferredSize(
        preferredSize: Size.fromHeight(44),
        child: Theme(
          data: ThemeData(
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent),
          child: TabBar(
            tabs: tabs,
            labelColor: rgba(136, 175, 213, 1),
            labelStyle: TextStyle(fontWeight: FontWeight.bold),
            unselectedLabelStyle: TextStyle(fontSize: 15),
            unselectedLabelColor: rgba(80, 80, 80, 1),
            controller: _tabController,
          ),
        ),
      ),
      backgroundColor: Colors.transparent,
    );
  }

  @override
  Widget build(BuildContext context) {
    // ScreenUtil.init(context,
    //     designSize: Size(750, 1334), allowFontScaling: false);
    return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
          image: AssetImage("lib/assets/images/tz/bg.png"),
          fit: BoxFit.cover,
        )),
        child: Scaffold(
            resizeToAvoidBottomInset: false,
            backgroundColor: Colors.transparent,
            appBar: createAppBar(),
            body: Container(
              color: Colors.transparent,
              child: TabBarView(
                children: <Widget>[
                  tab0(),
                  tab1(),
                  tab2(),
                ],
                controller: _tabController,
              ),
            )));
  }

  doZd(BuildContext context) async {
    G.loading.show(context);
    try {
      int adminid = G.user.data.adminid;
      Response response;
      Dio dio = Dio();
      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/jtorderzd",
          queryParameters: {"id": _jtinfo.id, "adminid": adminid});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('機台無回應訊息!');
        G.loading.hide(context);
        return null;
      }

      _istop = true;

      setState(() {});

      G.loading.hide(context);
      G.toast('機台置頂成功!');
      return null;
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  //修改机台名称
  doXgjtname(BuildContext context) async {
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post("https://aps.acciot.com.tw/api/taizhu/xgjtname",
          queryParameters: {"id": _jtinfo.id, "name": _name});

      if (response == null) {
        return;
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        return;
      }

      setState(() {});
      return null;
    } catch (e) {}
  }

  //删除机台
  deleteJt(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post("https://aps.acciot.com.tw/api/taizhu/delJt",
          queryParameters: {"id": _jtinfo.id});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('機台無回應訊息!');
        G.loading.hide(context);
        return null;
      }
      await G.toast('刪除機台成功!');
      Navigator.pop(context);
      G.loading.hide(context);
      return null;
    } catch (e) {
      G.loading.hide(context);
    }
  }

  //设置
  doSend(BuildContext context) async {
    if (_bjje < 0 || _bjje > 10000) {
      return G.toast('設置保夾金額錯誤!');
    }

    if (_name.length < 2) {
      return G.toast('請輸入2個中文或4個英數字以上機台名稱！');
    }

    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      response = await dio.post("https://aps.acciot.com.tw/api/taizhu/setbjje",
          queryParameters: {"id": _jtinfo.id, "bjje": _bjje, "name": _name});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.loading.hide(context);
        G.toast('機台無回應訊息!');
        return null;
      }
      await G.toast('機台設置成功訊息!');

      G.loading.hide(context);
      return null;
    } catch (e) {
      G.loading.hide(context);
    }
  }

  //tab1 开关选项
  doSave2(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();

      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/postjtinfo2",
          queryParameters: {
            "id": _jtinfo.id,
            "toubi_switch": _jtinfo.toubi_switch,
            "chuhuo_switch": _jtinfo.chuhuo_switch
          });

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('機台無回應訊息!');
        G.loading.hide(context);
        return null;
      }
      await G.toast('機台設置成功訊息!');

      G.loading.hide(context);
      return null;
    } catch (e) {
      G.loading.hide(context);
    }
  }

  //tab1
  doSave(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();

      int time = _hour * 60 + _miniute;

      response = await dio.post(
          "https://aps.acciot.com.tw/api/taizhu/postjtinfo",
          queryParameters: {
            "id": _jtinfo.id,
            "name": _jtinfo.name,
            "auto_switch": _jtinfo.auto_switch,
            "time": time,
            "money": _jtinfo.money,
            "zdxb": _zdxb,
          });

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.toast('機台無回應訊息!');
        G.loading.hide(context);
        return null;
      }
      await G.toast('機台設置成功訊息!');

      G.loading.hide(context);
      return null;
    } catch (e) {
      G.loading.hide(context);
    }
  }

  jtreset(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();

      int time = _hour * 60 + _miniute;

      response = await dio.post("https://aps.acciot.com.tw/api/taizhu/jtreset",
          queryParameters: {"id": _jtinfo.id});

      if (response == null) {
        G.loading.hide(context);
        return G.toast('網絡無連接！');
      }

      Map<String, dynamic> resCallbackMap = response.data;

      int code = resCallbackMap['code'];
      if (code != 1) {
        G.loading.hide(context);
        G.toast('重啟失败!');
        return null;
      }
      await G.toast('重啟成功!');

      G.loading.hide(context);
      return null;
    } catch (e) {
      G.loading.hide(context);
    }
  }

  Widget tab0() {
    return Container(
      padding: EdgeInsets.only(left: 0, right: 0),
      child: Column(
        children: <Widget>[
          ARow(
            color: Colors.transparent,
            height: 80,
            leftChild: Text(
              '機台列表置頂',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: null,
            rightChild: Switch(
              value: _istop,
              activeColor: Colors.red,
              onChanged: (value) {
                ADialog.confirm(context, content: '確認置頂?',
                    confirmButtonPress: () {
                  Future.delayed(Duration.zero, () {
                    doZd(context);
                    setState(() {
                      _istop = value;
                    });
                  });
                });
              },
            ),
          ),
          ARow(
            color: Colors.transparent,
            height: 80,
            leftChild: Text(
              '機台名稱',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: Container(
              height: 50,
              margin: EdgeInsets.only(left: 20),
              decoration: new BoxDecoration(
                  border: new Border.all(color: Color(0x66000000), width: 0.5),
                  color: Color(0xFFFFFF),
                  borderRadius: new BorderRadius.circular((5.0))),
              child: TextField(
                keyboardType: TextInputType.text,
                controller: _controller_jtname,
                decoration: InputDecoration(
                    counterText: "",
                    contentPadding: EdgeInsets.only(left: 10),
                    border: InputBorder.none,
                    hintText: "請輸入2個中文或4個英數字以上名稱",
                    hintStyle: TextStyle(
                      fontSize: 14,
                    )),
                onChanged: (e) {
                  setState(() {
                    _name = e;
                  });
                },
              ),
            ),
          ),
          ARow(
              color: Colors.transparent,
              height: 80,
              leftChild: Text(
                '保夾金額',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              centerChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 20),
                decoration: new BoxDecoration(
                    border:
                        new Border.all(color: Color(0x66000000), width: 0.5),
                    color: Color(0xFFFFFF),
                    borderRadius: new BorderRadius.circular((5.0))),
                child: TextField(
                  keyboardType: TextInputType.text,
                  controller: _controller_bjje,
                  decoration: InputDecoration(
                      counterText: "",
                      contentPadding: EdgeInsets.only(left: 10),
                      border: InputBorder.none,
                      hintText: "請輸入保夾金額",
                      hintStyle: TextStyle(
                        fontSize: 14,
                      )),
                  onChanged: (e) {
                    setState(() {
                      _bjje = int.parse(e);
                    });
                  },
                ),
              ),
              rightChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 8),
                child: AButton.normal(
                    child: Text('設置',
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold)),
                    type: 'info',
                    onPressed: () {
                      if (_jtinfo.isonline != 1) {
                        G.toast('機台已離線!');
                      } else {
                        ADialog.confirm(context, content: '確認設置此機臺?',
                            confirmButtonPress: () {
                          Future.delayed(Duration.zero, () {
                            doSend(context);
                          });
                        });
                      }
                    }),
              )),
          ARow(
              color: Colors.transparent,
              height: 195,
              leftChild: Container(
                margin: EdgeInsets.symmetric(vertical: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    AButton.normal(
                        width: MediaQuery.of(context).size.width - 30,
                        child: Text('操作記錄',
                            style: TextStyle(
                                fontSize: 18, fontWeight: FontWeight.bold)),
                        type: 'info',
                        onPressed: () => G.pushNamed('/czjl')),
                    Text(''),
                    Text('廠商: DF Co.,LTD',
                        textAlign: TextAlign.left,
                        style: TextStyle(fontSize: 16, height: 1.6)),
                    Text('型號: FE1000',
                        textAlign: TextAlign.left,
                        style: TextStyle(fontSize: 16, height: 1.6)),
                    Text('設備ID: ${_jtinfo.jt_serial}',
                        textAlign: TextAlign.left,
                        style: TextStyle(fontSize: 16, height: 1.6)),
                    Text('到期時間: ${_jtinfo.expiration_date}',
                        textAlign: TextAlign.left,
                        style: TextStyle(fontSize: 16, height: 1.6)),
                  ],
                ),
              )),
          Container(
              margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
              width: double.infinity,
              child: AButton.normal(
                  child: Text('删除機台',
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                  type: 'danger',
                  onPressed: () {
                    ADialog.confirm(context, content: '確認删除機台?',
                        confirmButtonPress: () {
                      Future.delayed(Duration.zero, () {
                        deleteJt(context);
                      });
                    });
                  })),
        ],
      ),
    );
  }

  Widget tab1() {
    return Container(
      padding: EdgeInsets.only(left: 0, right: 0),
      child: Column(
        children: <Widget>[
          ARow(
            height: 60,
            color: Colors.transparent,
            leftChild: Text(
              '投幣提示',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: null,
            rightChild: Switch(
              value: _jtinfo.toubi_switch == 1 ? true : false,
              activeColor: Colors.red,
              onChanged: (value) {
                if (_jtinfo.isonline != 1) {
                  G.toast('機台已離線!');
                } else {
                  ADialog.confirm(context, content: '確認修改?',
                      confirmButtonPress: () {
                    Future.delayed(Duration.zero, () {
                      setState(() {
                        if (value) {
                          _jtinfo.toubi_switch = 1;
                        } else {
                          _jtinfo.toubi_switch = 0;
                        }
                        doSave2(context);
                      });
                    });
                  });
                }
              },
            ),
          ),
          ARow(
            height: 60,
            color: Colors.transparent,
            leftChild: Text(
              '出貨提示',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: null,
            rightChild: Switch(
              value: _jtinfo.chuhuo_switch == 1 ? true : false,
              activeColor: Colors.red,
              onChanged: (value) {
                if (_jtinfo.isonline != 1) {
                  G.toast('機台已離線!');
                } else {
                  ADialog.confirm(context, content: '確認修改?',
                      confirmButtonPress: () {
                    Future.delayed(Duration.zero, () {
                      setState(() {
                        if (value) {
                          _jtinfo.chuhuo_switch = 1;
                        } else {
                          _jtinfo.chuhuo_switch = 0;
                        }
                        doSave2(context);
                      });
                    });
                  });
                }
              },
            ),
          ),
          ARow(
            color: Colors.transparent,
            height: 60,
            leftChild: Text(
              '自動消保',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: null,
            rightChild: Switch(
              value: _jtinfo.auto_switch == 1 ? true : false,
              activeColor: Colors.red,
              onChanged: (value) {
                setState(() {
                  if (value) {
                    _jtinfo.auto_switch = 1;
                  } else {
                    _jtinfo.auto_switch = 0;
                  }
                });
              },
            ),
          ),
          ARow(
            color: Colors.transparent,
            height: 80,
            leftChild: Text(
              '自動消保金额',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: Container(
              height: 50,
              margin: EdgeInsets.only(left: 20),
              decoration: new BoxDecoration(
                  border: new Border.all(color: Color(0x66000000), width: 0.5),
                  color: Color(0xFFFFFF),
                  borderRadius: new BorderRadius.circular((5.0))),
              child: TextField(
                keyboardType: TextInputType.text,
                controller: _controller_zdxb,
                decoration: InputDecoration(
                    counterText: "",
                    contentPadding: EdgeInsets.only(left: 10),
                    border: InputBorder.none,
                    hintText: "0~9990",
                    hintStyle: TextStyle(
                      fontSize: 14,
                    )),
                onChanged: (e) {
                  setState(() {
                    _zdxb = int.parse(e);
                    if (_zdxb > 9990) {
                      G.toast('自動消保金額不能大于9990!');
                    }
                  });
                },
              ),
            ),
          ),
          ARow(
            height: 80,
            color: Colors.transparent,
            leftChild: Text(
              '自動消保時間',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Container(
                    height: 50,
                    margin: EdgeInsets.only(left: 20),
                    decoration: new BoxDecoration(
                        border: new Border.all(
                            color: Color(0x66000000), width: 0.5),
                        color: Color(0xFFFFFF),
                        borderRadius: new BorderRadius.circular((5.0))),
                    child: TextField(
                      keyboardType: TextInputType.text,
                      controller: _controller_hour,
                      decoration: InputDecoration(
                          counterText: "",
                          contentPadding: EdgeInsets.only(left: 10),
                          border: InputBorder.none,
                          hintText: "小時",
                          hintStyle: TextStyle(
                            fontSize: 14,
                          )),
                      onChanged: (e) {
                        setState(() {
                          _hour = int.parse(e);
                        });
                      },
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    height: 50,
                    margin: EdgeInsets.only(left: 20),
                    decoration: new BoxDecoration(
                        border: new Border.all(
                            color: Color(0x66000000), width: 0.5),
                        color: Color(0xFFFFFF),
                        borderRadius: new BorderRadius.circular((5.0))),
                    child: TextField(
                      keyboardType: TextInputType.text,
                      controller: _controller_miniute,
                      decoration: InputDecoration(
                          counterText: "",
                          contentPadding: EdgeInsets.only(left: 10),
                          border: InputBorder.none,
                          hintText: "分鐘",
                          hintStyle: TextStyle(
                            fontSize: 14,
                          )),
                      onChanged: (e) {
                        setState(() {
                          _miniute = int.parse(e);
                        });
                      },
                    ),
                  ),
                ),
              ],
            ),
            // rightChild: null,
          ),
          Container(
              margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
              width: double.infinity,
              child: AButton.normal(
                  child: Text('重啟機台',
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                  type: 'danger',
                  onPressed: () {
                    if (_jtinfo.isonline != 1) {
                      G.toast('機台已離線!');
                    } else {
                      ADialog.confirm(context, content: '確認重啟機台?',
                          confirmButtonPress: () {
                        Future.delayed(Duration.zero, () {
                          jtreset(context);
                        });
                      });
                    }
                  })),
          Container(
              margin: EdgeInsets.symmetric(horizontal: 10),
              width: double.infinity,
              child: AButton.normal(
                  child: Text('保存',
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                  type: 'info',
                  onPressed: () {
                    if (_jtinfo.isonline != 1) {
                      G.toast('機台已離線!');
                    } else {
                      ADialog.confirm(context, content: '確認保存修改?',
                          confirmButtonPress: () {
                        Future.delayed(Duration.zero, () {
                          doSave(context);
                        });
                      });
                    }
                  })),
        ],
      ),
    );
  }

  Widget tab2() {
    return Container(
      padding: EdgeInsets.only(left: 0, right: 0),
      child: Column(
        children: <Widget>[
          ARow(
            color: Colors.transparent,
            height: 80,
            leftChild: Text(
              '起始日期',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: Container(
              height: 50,
              margin: EdgeInsets.only(left: 37),
              decoration: new BoxDecoration(
                  border: new Border.all(color: Color(0x66000000), width: 0.5),
                  color: Color(0xFFFFFF),
                  borderRadius: new BorderRadius.circular((5.0))),
              child: new MaterialButton(
                child: Text(_xzrq0, style: TextStyle(fontSize: 18)),
                onPressed: () {
                  // 调用函数打开
                  showDatePicker(
                    context: context,
                    initialDate: new DateTime.now(),
                    firstDate: new DateTime.now()
                        .subtract(new Duration(days: 6000)), // 减 30 天
                    lastDate: new DateTime.now()
                        .add(new Duration(days: 30)), // 加 30 天
                  ).then((DateTime val) {
                    setState(() {
                      _xzrq0 = DateFormat('yyyy-MM-dd').format(val);
                    });
                  }).catchError((err) {
                    print(err);
                  });
                },
              ),
            ),
          ),
          ARow(
            color: Colors.transparent,
            height: 60,
            leftChild: Text(
              '結束日期',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: Container(
              height: 50,
              margin: EdgeInsets.only(left: 37),
              decoration: new BoxDecoration(
                  border: new Border.all(color: Color(0x66000000), width: 0.5),
                  color: Color(0xFFFFFF),
                  borderRadius: new BorderRadius.circular((5.0))),
              child: new MaterialButton(
                child: Text(_xzrq1, style: TextStyle(fontSize: 18)),
                onPressed: () {
                  // 调用函数打开
                  showDatePicker(
                    context: context,
                    initialDate: new DateTime.now(),
                    firstDate: new DateTime.now()
                        .subtract(new Duration(days: 6000)), // 减 6000 天
                    lastDate: new DateTime.now()
                        .add(new Duration(days: 30)), // 加 30 天
                  ).then((DateTime val) {
                    setState(() {
                      _xzrq1 = DateFormat('yyyy-MM-dd').format(val);
                    });
                  }).catchError((err) {
                    print(err);
                  });
                },
              ),
            ),
          ),
          ARow(
            color: Colors.transparent,
            height: 60,
            leftChild: Text(
              '總投幣',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: Container(
              height: 50,
              margin: EdgeInsets.only(left: 53),
              decoration: new BoxDecoration(
                  border: new Border.all(color: Color(0x66000000), width: 0.5),
                  color: Color(0xFFFFFF),
                  borderRadius: new BorderRadius.circular((5.0))),
              child: TextField(
                keyboardType: TextInputType.text,
                enabled: false,
                controller: _controller_ztbs,
                decoration: InputDecoration(
                    counterText: "",
                    contentPadding: EdgeInsets.only(left: 10),
                    border: InputBorder.none,
                    hintText: "請輸入總投幣",
                    hintStyle: TextStyle(
                      fontSize: 14,
                    )),
                onChanged: (e) {
                  setState(() {});
                },
              ),
            ),
          ),
          ARow(
            color: Colors.transparent,
            height: 60,
            leftChild: Text(
              '總出貨數',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            centerChild: Container(
              height: 50,
              margin: EdgeInsets.only(left: 36),
              decoration: new BoxDecoration(
                  border: new Border.all(color: Color(0x66000000), width: 0.5),
                  color: Color(0xFFFFFF),
                  borderRadius: new BorderRadius.circular((5.0))),
              child: TextField(
                enabled: false,
                keyboardType: TextInputType.text,
                controller: _controller_zchs,
                decoration: InputDecoration(
                    counterText: "",
                    contentPadding: EdgeInsets.only(left: 10),
                    border: InputBorder.none,
                    hintText: "請輸入總出貨數",
                    hintStyle: TextStyle(
                      fontSize: 14,
                    )),
                onChanged: (e) {
                  setState(() {});
                },
              ),
            ),
          ),
          Container(
              margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
              width: double.infinity,
              child: AButton.normal(
                  child: Text('查詢',
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                  type: 'danger',
                  onPressed: () {
                    qingchu(context);
                  })),
          ARow(
              color: Colors.transparent,
              height: 60,
              leftChild: Text(
                '投幣累進',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              centerChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 20),
                decoration: new BoxDecoration(
                    border:
                        new Border.all(color: Color(0x66000000), width: 0.5),
                    color: Color(0xFFFFFF),
                    borderRadius: new BorderRadius.circular((5.0))),
                child: TextField(
                  enabled: false,
                  keyboardType: TextInputType.text,
                  controller: _controller_tblj,
                  decoration: InputDecoration(
                      counterText: "",
                      contentPadding: EdgeInsets.only(left: 10),
                      border: InputBorder.none,
                      hintText: "請輸入投幣累進",
                      hintStyle: TextStyle(
                        fontSize: 14,
                      )),
                  onChanged: (e) {
                    setState(() {});
                  },
                ),
              ),
              rightChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 8),
                child: AButton.normal(
                    child: Text('歸零',
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold)),
                    type: 'danger',
                    onPressed: () {
                      if (_jtinfo.isonline != 1) {
                        G.toast('機台已離線!');
                      } else {
                        ADialog.confirm(context, content: '確認投幣累進歸零?',
                            confirmButtonPress: () {
                          Future.delayed(Duration.zero, () {
                            doResettblj(context);
                          });
                        });
                      }
                    }),
              )),
          ARow(
              color: Colors.transparent,
              height: 60,
              leftChild: Text(
                '出貨累進',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              centerChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 20),
                decoration: new BoxDecoration(
                    border:
                        new Border.all(color: Color(0x66000000), width: 0.5),
                    color: Color(0xFFFFFF),
                    borderRadius: new BorderRadius.circular((5.0))),
                child: TextField(
                  enabled: false,
                  keyboardType: TextInputType.text,
                  controller: _controller_chlj,
                  decoration: InputDecoration(
                      counterText: "",
                      contentPadding: EdgeInsets.only(left: 10),
                      border: InputBorder.none,
                      hintText: "請輸入出貨累進",
                      hintStyle: TextStyle(
                        fontSize: 14,
                      )),
                  onChanged: (e) {
                    setState(() {});
                  },
                ),
              ),
              rightChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 8),
                child: AButton.normal(
                    child: Text('歸零',
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold)),
                    type: 'danger',
                    onPressed: () {
                      if (_jtinfo.isonline != 1) {
                        G.toast('機台已離線!');
                      } else {
                        ADialog.confirm(context, content: '確認出貨累進歸零?',
                            confirmButtonPress: () {
                          Future.delayed(Duration.zero, () {
                            doResetchlj(context);
                          });
                        });
                      }
                    }),
              )),
          ARow(
              color: Colors.transparent,
              height: 80,
              leftChild: Text(
                '商品成本',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              centerChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 20),
                decoration: new BoxDecoration(
                    border:
                    new Border.all(color: Color(0x66000000), width: 0.5),
                    color: Color(0xFFFFFF),
                    borderRadius: new BorderRadius.circular((5.0))),
                child: TextField(
                  keyboardType: TextInputType.text,
                  controller: _controller_cost,
                  decoration: InputDecoration(
                      counterText: "",
                      contentPadding: EdgeInsets.only(left: 10),
                      border: InputBorder.none,
                      hintText: "請輸入商品成本",
                      hintStyle: TextStyle(
                        fontSize: 14,
                      )),
                  onChanged: (e) {
                    setState(() {
                      _cost = int.parse(e);
                    });
                  },
                ),
              ),
              rightChild: Container(
                height: 50,
                margin: EdgeInsets.only(left: 8),
                child: AButton.normal(
                    child: Text('設置',
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold)),
                    type: 'info',
                    onPressed: () {
                      ADialog.confirm(context, content: '確認設置此機臺商品成本?',
                          confirmButtonPress: () {
                            Future.delayed(Duration.zero, () {
                              doSetCost(context);
                            });
                          });
                    }),
              )),
        ],
      ),
    );
  }
}
